import $service from '../../../../service.js';
var user_type;
var user_id;
var log_id;
var hospital_id ="1";
var inquiry_patient_id;
var data_id;
var isMeasured;
var that;
var doctor_id;
Page({
    data:{
        measuredDataMap_user:{
            data_animal_heat:'',//体温
            data_sbp: '',//收缩压
            data_dbp: '',//舒张压
            data_heart_rate: '',//心率
            data_rr: '',//呼吸频率
            data_glu: '',//血糖
            data_condition: '请选择',//	测量条件
            data_parameter: '',//测量参数
        },
        list:[],
        isshow:false
    },
    onLoad(e){
        log_id = e.log_id
    },
    onShow(){
        that=this.data;
        if(!$service.isUndefinedAndEmpty($service.getUserLocalStorage().user_type)){
        		user_type = $service.getUserLocalStorage().user_type;
        	}
         if(user_type == 1){//1用户 1门店
             user_id = $service.getUserLocalStorage().user_id;
         }else{
             user_id = $service.getUserLocalStorage().store_id;
         }
        this.publicUtil_getUtilTable()
        this.publicUtil_getPatientMeasuredDataList()
    },
    //获取测量数据
    publicUtil_getPatientMeasuredDataList:function(){
        var jsonData ={
            "is_receive" : '1',
            "log_id" : log_id
        }
        console.log(jsonData)
        var jsonAjax = {
            "url": "publicUtil_getInquiryLogDetails.do",
            "jsonData": jsonData,
        }
        $service.getAjaxData(jsonAjax, this.publicUtil_getPatientMeasuredDataList_back);
    },
    publicUtil_getPatientMeasuredDataList_back:function(jsonBack){
        var data=$service.strToJson(jsonBack.data).data; 
        isMeasured = data.isMeasured;
        doctor_id = data.doctorMap.doctor_id
        inquiry_patient_id = data.inquiryMap.inquiry_patient_id;
        // if(!$service.isUndefinedAndEmpty(data.measuredDataMap.lrsj)){
        //     data.measuredDataMap.lrsj = data.measuredDataMap.lrsj.substring(0, 10);
        //     }
            if($service.isUndefinedAndEmpty(data.measuredDataMap.data_condition)){//	测量条件
                data.measuredDataMap.data_condition = '请选择'
            }
        // }else{
        //     data.measuredDataMap = this.data.measuredDataMap_user
        // }
        this.setData({
            measuredDataMap_user : data.measuredDataMap,
        })
    },
    
    //获取测量条件
    publicUtil_getUtilTable:function(){
        var jsonData ={
            "type" : "1",//类型 1血糖测量条件 2药品用法 3药品使用频率 4系统医院 5科室表 多个','号隔开
            "hospital_id" : hospital_id,//医院id
        }
        console.log(jsonData)
        var jsonAjax = {
            "url": "publicUtil_getUtilTable.do",
            "jsonData": jsonData,
        }
        $service.getAjaxData(jsonAjax, this.publicUtil_getUtilTable_back);
    },
    publicUtil_getUtilTable_back:function(jsonBack){
        var data=$service.strToJson(jsonBack.data).data; 
        var list = data.list;
        this.setData({
            list : list
        })
    },
    //选择测量条件
    tapisshow(e){
        console.log(e)
        var index = e.currentTarget.dataset.index;
        if(index == 1){
            this.data.isshow = !this.data.isshow
        }else if(index == 2){
            var num = e.currentTarget.dataset.num;
            this.data.measuredDataMap_user.data_condition = this.data.list[num].condition_name;
            this.data.isshow =  false;
        }
        this.setData({
            isshow : this.data.isshow,
            measuredDataMap_user : this.data.measuredDataMap_user
        })
    },
    inputChange(e){
        var index = e.currentTarget.dataset.index;
        if(index == 1){//体温
            this.data.measuredDataMap_user.data_animal_heat = e.detail.value
        }else if(index == 2){//心率
             this.data.measuredDataMap_user.data_heart_rate = e.detail.value
        }else if(index == 3){//收缩压
             this.data.measuredDataMap_user.data_sbp = e.detail.value
        }else if(index == 4){//舒张压
             this.data.measuredDataMap_user.data_dbp = e.detail.value
        }else if(index == 5){//呼吸频率
             this.data.measuredDataMap_user.data_rr = e.detail.value
        }else if(index == 6){//血糖
             this.data.measuredDataMap_user.data_glu = e.detail.value
        }else if(index == 7){//测量参数
             this.data.measuredDataMap_user.data_parameter = e.detail.value
        }
        this.setData({
            measuredDataMap_user : this.data.measuredDataMap_user
        })
    },
    submission(e){
        var index = e.currentTarget.dataset.index;
        if(index == 1){//录入数据
            if(!$service.isUndefinedAndEmpty(this.data.measuredDataMap_user.data_sbp) ){
                if(this.data.measuredDataMap_user.data_dbp == ''){
                    $service.showToast('有填写收缩压就必须填写舒张压');
                    return;
                }
            }
            if(!$service.isUndefinedAndEmpty(this.data.measuredDataMap_user.data_dbp) ){
                if(this.data.measuredDataMap_user.data_sbp == ''){
                    $service.showToast('有填写舒张压就必须填写收缩压');
                    return;
                }
            }
            if(Number(this.data.measuredDataMap_user.data_sbp) < Number(this.data.measuredDataMap_user.data_dbp)){
                $service.showToast('收缩压必须大于舒张压');
                return;
            }
            if(!$service.isUndefinedAndEmpty(this.data.measuredDataMap_user.data_glu) ){
                if(this.data.measuredDataMap_user.data_condition == '请选择'){
                    $service.showToast('录入血糖必须选择测量条件');
                    return;
                }
            }
            if($service.isUndefinedAndEmpty(this.data.measuredDataMap_user.data_animal_heat) && $service.isUndefinedAndEmpty(this.data.measuredDataMap_user.data_sbp) && $service.isUndefinedAndEmpty(this.data.measuredDataMap_user.data_dbp) && $service.isUndefinedAndEmpty(this.data.measuredDataMap_user.data_heart_rate) && $service.isUndefinedAndEmpty(this.data.measuredDataMap_user.data_rr) && $service.isUndefinedAndEmpty(this.data.measuredDataMap_user.data_glu) && $service.isUndefinedAndEmpty(this.data.measuredDataMap_user.data_parameter)){
                $service.showToast('至少要输入一项测量内容');
                return;
            }
            if(this.data.measuredDataMap_user.data_condition == '请选择'){//	测量条件
                var data_condition = ''
            }else{
                var data_condition = this.data.measuredDataMap_user.data_condition
            }
            var jsonData ={
                "log_id" : log_id,
                "hospital_id" :hospital_id,//医院id
                "inquiry_patient_id" : inquiry_patient_id,//患者id
                "user_type" : user_type,//用户id 1用户 2门店
                "user_id": user_id,//	用户主键
                "data_animal_heat":this.data.measuredDataMap_user.data_animal_heat,//体温
                "data_sbp": this.data.measuredDataMap_user.data_sbp,//收缩压
                "data_dbp": this.data.measuredDataMap_user.data_dbp,//舒张压
                "data_heart_rate":this.data.measuredDataMap_user.data_heart_rate,//心率
                "data_rr": this.data.measuredDataMap_user.data_rr,//呼吸频率
                "data_glu": this.data.measuredDataMap_user.data_glu,//血糖
                "data_condition": data_condition,//	测量条件
                "data_parameter": this.data.measuredDataMap_user.data_parameter,//测量参数
            }
            if(isMeasured == 1){
                jsonData.data_id = this.data.measuredDataMap_user.data_id;
                var url = "publicUtil_updateInquiryPatientMeasuredData.do"
            }else{
                var url = "publicUtil_addPatientMeasuredData.do"
            }
            console.log(jsonData)
            var jsonAjax = {
                "url": url,
                "jsonData": jsonData,
            }
            $service.getAjaxData(jsonAjax, function(jsonObj){
                var selToID='D' + doctor_id;
                var sendSystemMessageMsgText=''
                if(isMeasured == 1){
                    var sendSystemMessageMsgText = '用户修改测量数据'
                    $service.showToast('修改成功')
                }else{
                    var sendSystemMessageMsgText = '用户录入测量数据'
                    $service.showToast('录入成功')
                }
                wx.txTic.sendCustomMessage(selToID,3)
                wx.txTic.sendSystemMessageMsg(selToID,sendSystemMessageMsgText);
                
                setTimeout(function() {
                    wx.navigateBack({
                        delta:1
                    })
                }, 1000);
            });
        }else if(index == 2){
            var inquiryinfo = {
                "inquiry_patient_id" : inquiry_patient_id,
                "log_id" : log_id
            }
            wx.navigateTo({
                url: '../index_measureddata/index_measureddata?inquiryinfo=' + $service.jsonToStr(inquiryinfo)
            })
        }
    }
    
})